#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>

#include "Utils\Utils.h"
#include "List\LIST.h"
#include "Clients\Clients.h"
#include "Properties\Properties.h"
#include "Matching\Matching.h"



#pragma warning (disable: 4996)

// Declarations of the function definitions.

typedef void (*MainMenuClientFunction)(Client **ClientsArray,int *ClientCount,VoidCallBack CB);

typedef void (*MainMenuPropertyFunction)(List *PropertiesList,int *PropertiesCount);

typedef void (*MainMenuClientPropertyFunction)(Client *ClientsArray,int *ClientCount,
											   List PropertiesList,int *PropertiesCount);

// Const arrays of the functions, later called by the selection
// of main menu of the user choise.
const
MainMenuClientPropertyFunction MainFunctions[MenuSize-1]={
	NULL,NULL,WriteFiles,Show_Matching_Menu,Show_Reports};

MainMenuClientFunction ClientFunctions[MenuSize-1]={
	Add_New_Client,UpdateClients,DeleteClients,ShowAllClients,SearchClients};

MainMenuPropertyFunction PropertiesFunctions[MenuSize-1]={
	Add_New_Property,Update_Property,Delete_Property,Show_All_Properties,Find_Property};


void main()
{
   HANDLE EyesThread;
   DWORD ThreadID;
      	  	
   int i=0,j=0;
   Client *ClientsArray=NULL;
   List PropertiesList=NULL;
   int ClientCount=0;
   int PropertiesCount=0;

   EyesThread = CreateThread(NULL,0,StartThread,NULL,0,&ThreadID);
   SuspendThread(EyesThread);

   if (!UserCheckPassword()) exit(1); //LPH54
   ClientsArray = LoadClientsFromFile(&ClientCount);
   PropertiesList = LoadPropertiesFromFile(&PropertiesCount);
   showcursor(FALSE);
   do
   {
   ResumeThread(EyesThread);
   Sleep(100);
   i=menu(mainstrings,MenuSize,i,"Main",MainMenuStartY);
   SuspendThread(EyesThread);
   if (i==0)
   {j=0;
	   do
	   {
		   j=menu(clientstrings,MenuSize,j,"Clients",MainMenuStartY);
		   if (j>=0 && j<=MenuSize-2)
			   ClientFunctions[j](&ClientsArray,&ClientCount,MainMenuFadeCallBack);
	   }
	   while(j!=5 && j!=-1);
   }
   if (i==1)
   {j=0;
	   do
	   {
		   j=menu(PropertiesStrings,PropertiesStringsLength,j,"Properties",MainMenuStartY);
		   if (j>=0 && j<=MenuSize-2)
			   PropertiesFunctions[j](&PropertiesList,&PropertiesCount);
	
	   }
	   while(j!=5 && j!=-1);
   }

   if (i>=2 && i<=4)
	   MainFunctions[i](ClientsArray,&ClientCount,PropertiesList,&PropertiesCount);

    if (i==-1 || i==5)
	   if (!AskYesNoQuestion("Are You Sure You Want To Quit ?"))
		   i=0;		
  
   }
   while(i!=5 && i!=-1);
   
   FreeFiles(ClientsArray,&ClientCount,PropertiesList,&PropertiesCount);
   fadeonquit();
   printf("bye bye\n");
}